<template>
  <div class="touzilist">
    <el-card>
      <el-row :gutter="10" class="elrow">
        <el-col :span="2">
          <el-tag>课程名</el-tag>
        </el-col>
        <el-col :span="4">
          <el-input placeholder="请输入课程名" v-model="formData.cname" maxlength="18"></el-input>
        </el-col>
        <el-col :span="2">
          <el-tag>课程类型</el-tag>
        </el-col>
        <el-col :span="4">
          <el-select v-model="formData.level" clearable placeholder="请选择课程类型">
            <el-option value="1" label="专升本"></el-option>
            <el-option value="2" label="高起专"></el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-tag>学习方式</el-tag>
        </el-col>
        <el-col :span="4">
          <el-select v-model="formData.cway" clearable placeholder="请选学习方式">
            <el-option value="1" label="函授"></el-option>
            <el-option value="2" label="业余"></el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" size="medium" icon="el-icon-search" v-on:click="getTabsList">搜索</el-button>
        </el-col>
        <el-col :span="3">
          <el-button  size="medium" icon="el-icon-download" v-on:click="export2Excel">Excle</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)" style="width: 100%" border>
          <el-table-column :index="indexMethod" type="index" label="序号" width="50"></el-table-column>
          <el-table-column label="课程名称" align="center" width="200">
            <template slot-scope="scope">
              <span>{{ scope.row.cname}}</span>
            </template>
          </el-table-column>
          <el-table-column label="课程图片" align="center" width="180">
            <template slot-scope="scope">
          <span v-if="scope.row.img == null || scope.row.img === ''">
            <el-image
              class="table-td-thumb"
              style="width: 80px; height: 80px"
              src="http://localhost:8080/CourseHead/zwtp.png"
            ></el-image>
          </span>
              <span v-else>
            <el-image
              class="table-td-thumb"
              style="width: 80px; height: 80px"
              :src="scope.row.img"
              :preview-src-list="[scope.row.img]"
            ></el-image>
          </span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" width="180">
            <template slot-scope="scope">
              <span style="color:rgb(0, 208, 83)">{{scope.row.cdate  | dataFormat}}</span>
            </template>
          </el-table-column>
          <el-table-column label="课程价格" align="center" width="180">
            <template slot-scope="scope">
            <span>
              <el-tag size="medium">￥{{ scope.row.cvalue}}</el-tag>
            </span>
            </template>
          </el-table-column>
          <el-table-column label="课程学时" align="center" width="150">
            <template slot-scope="scope">
              <span>{{ scope.row.chour}}学时</span>
            </template>
          </el-table-column>
          <el-table-column label="课程容量" align="center" width="150">
            <template slot-scope="scope">
              <span>{{ scope.row.cnum}}</span>
            </template>
          </el-table-column>
          <el-table-column label="课程层次" align="center" width="110">
            <template slot-scope="scope">
              <span v-if=" scope.row.level==1">专升本课程</span>
              <span v-else-if="scope.row.level === 2">高升本课程</span>
              <span v-else>高升专课程</span>
            </template>
          </el-table-column>
          <el-table-column label="面向类型" align="center" width="110">
            <template slot-scope="scope">
              <span v-if="scope.row.cway=='1'">函授类型</span>
              <span v-else>业余类型</span>
            </template>
          </el-table-column>
          <el-table-column label="课程审核状态" align="center" width="120">
            <template slot-scope="scope">
              <span v-if="scope.row.isverify==1">已审核</span>
              <span v-else-if="scope.row.isverify==0" style="color: #fc0d6f">未审核</span>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!--分页组件-->
      <div style="text-align: center;margin-top: 30px;">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pagesize"
          :total="total"
          :current-page="currentPage"
          @current-change="current_change">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
  import {export_json_to_excel} from "../../../excel/Export2Excel";
  import Vue from "vue";
  import moment from "moment";

  export default {
    data() {
      return {
        tableData: [],
        formData: {
          cname: '',
          level: '',
          cway: ''
        },
        total: 0,
        pagesize: 4,
        currentPage:1,
      };
    },
    methods: {
      current_change(val){
        this.currentPage = val;
        console.log(val);
        console.log(this.currentPage);
       // console.log(this.tableData);
      },
      getTabsList() {
        this.$axios
          .get("/querycourse", {
            params:{
              cname: this.formData.cname,
              cway: this.formData.cway,
              level: this.formData.level,
            }
          })
          .then(res => {
            //			console.log(res)
            const data = res.data;
            this.tableData = data.data;
            this.total = res.data.data.length;
            console.log(this.tableData)
          });
      },
      /**
       *  格式数据
       *  @filterVal  格式头
       *  @tableData  用来格式的表格数据
       */
      formatJson(filterVal, jsonData) {
        return jsonData.map(v =>
          filterVal.map(j => {
            if (j === "cdate") {
              return moment(v[j]).format("yyyy-MM-DD");
            } else {
              return v[j];
            }
          })
        );
      },
      outExe() {
        this.$confirm('此操作将导出excel文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.export2Excel()
        }).catch(() => {

        });
      },
      // 导出按钮点击事件函数
      export2Excel() {
        var tHeader = ['Cid', '课程名称', '创建时间', '课程价格', '课程学时', '课程容量','课程类型','面向类型','课程审核状态'];
        var filterVal = ['cid', 'cname', 'cdate','cvalue','chour','cnum','ctype','cway','isverify'];
        var filename = 'demoExcel';
        var data = this.formatJson(filterVal, this.tableData);
        export_json_to_excel(
          tHeader,
          data,
          filename
        );
      },
      indexMethod(index){
        return (this.currentPage-1)*this.pagesize+index+1;
      }
    },
    mounted() {
      this.getTabsList();
    },
  }
</script>

<style scoped>
  .touzilist {
    margin: 10px;
  }
  .elrow{
    margin-bottom: 20px;
  }
</style>
